<template>
  <section class="contianer">
    <el-row type="flex" justify="space-between">
      <!-- 顶部过滤列表 -->
      <div class="flights-content">
        <!-- 过滤条件 -->
        <FlightsFilters
          v-if="flightData.options"
          :data="flightData"
          @setFilteredList="setFilteredList"
        />

        <!-- 航班头部布局 -->
        <FlightsListHeaght />

        <!-- 处理空数据状态 -->
        <div v-if="flightData.flights && flightData.flights.length == 0">
          暂无数据
        </div>
        <!-- 航班信息 -->
        <FlightsItem v-for="item in dataList" :key="item.id" :data="item" />
        <el-pagination
          v-if="flightData.flights"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageIndex"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="filteredList.length"
        >
        </el-pagination>
      </div>

      <!-- 侧边栏 -->
      <div class="aside">
        <!-- 侧边栏组件 -->
        <FlightsAside />
      </div>
    </el-row>
  </section>
</template>

<script>
import FlightsListHeaght from "../../components/air/FlightsListHead";
import FlightsItem from "../../components/air/FlightsItem";
import FlightsFilters from "../../components/air/FlightsFilters";
import FlightsAside from "../../components/air/FlightsAside";
export default {
  data() {
    return {
      flightData: {}, //航班总数据
      filteredList: [],
      pageIndex: 1, // 默认当前页
      pageSize: 5, // 当前页有多少条
      total: 0, // 总页数

      // dataList: [], // 存当前页的数据
    };
  },
  components: {
    FlightsListHeaght,
    FlightsItem,
    FlightsFilters,
    FlightsAside,
  },

  created() {
    this.loadPage();
  },
  watch: {
    // 监听路径变化, 如果点击了搜索记录应该发起搜索请求
    $route() {
      this.loadPage();
    },
  },
  computed: {
    dataList() {
      // 不可以再用原100条数据做分页
      // 要用过滤后的数组分页
      if (!this.filteredList) {
        return [];
      }

      let beginIndex = (this.pageIndex - 1) * this.pageSize;
      let endIndex = beginIndex + this.pageSize;
      return this.filteredList.slice(beginIndex, endIndex);
    },
  },
  methods: {
    loadPage() {
      this.$axios({
        url: "/airs",
        params: this.$route.query,
      }).then((res) => {
        console.log(res.data);
        this.flightData = res.data;

        // 页面刚创建, 过滤器还没有任何选项
        // 先将100条作为过滤后的数据用来显示
        this.filteredList = this.flightData.flights;
      });
    },
    // 切换条数触发
    handleSizeChange(value) {
      this.pageSize = value;
      // this.setDataList();
    },

    // 切换页数时触发
    handleCurrentChange(value) {
      this.pageIndex = value;
      // this.setDataList();
    },

    // 设置机票列表数据
    // setDataList() {
    //   // 在总列表中截取当前页的数据
    //   this.dataList = this.flightData.flights.slice(
    //     (this.pageIndex - 1) * this.pageSize,
    //     this.pageIndex * this.pageSize
    //   );
    // },
    setFilteredList(newList) {
      this.filteredList = newList;
    },
  },
};
</script>

<style lang="less" scoped>
.contianer {
  width: 1000px;
  margin: 20px auto;
}

.flights-content {
  width: 745px;
  font-size: 14px;
}

.aside {
  width: 240px;
}
</style>